﻿Imports System.Runtime.CompilerServices
Imports System.Linq

Public Module ExtendedMLCategoryExtensions

    <Extension()>
    Public Function PopulatePaths(ByRef CatList As List(Of ExtendedMLCategory)) As List(Of ExtendedMLCategory)

        For Each c As ExtendedMLCategory In CatList
            c.Path = String.Format("{0}\{1}", GetParentCategory(CatList, c.ParentRef), c.Title)
        Next
        Return CatList

    End Function

    Private Function GetParentCategory(ByRef CatList As List(Of ExtendedMLCategory), ByVal ParentRef As String) As String
        If ParentRef = 0 Or ParentRef = -1 Then Return String.Empty
        Dim mlcat As ExtendedMLCategory = CatList.First(Function(cat) cat.ID = ParentRef)

        If mlcat Is Nothing Then Return String.Empty
        Return String.Format("{0}\{1}", _
                             GetParentCategory(CatList, mlcat.ParentRef), _
                             mlcat.Title)
    End Function

End Module
